﻿using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using SimpleTool.Server;
using System.Windows.Media.Animation;
using SimpleTool.TransformComponent;

namespace SimpleTool;


/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : EnjoyWindow
{
    private readonly MainViewModel? MainViewModel = null;

    private readonly Ruined? Asbin = null;

    private DoubleAnimation? CloseAnimation = null;

    public MainWindow()
    {
        InitializeComponent(); //  窗口初始化

        App MyApp = (App)Application.Current;

        this.Title = "SimpleTools";

        MainViewModel = MyApp.ServiceProvider.GetRequiredService<MainViewModel>();

        DataContext = MainViewModel; //  设置上下文为当前窗口

        Asbin = new Ruined(this, () =>
        {
            MyApp.MyLog.Dispose();
        },
        async Task<bool> () =>
        {
            TaskCompletionSource source = new ();

            void Closed(object? sender, EventArgs e)
            {
                Log.Logger.Print(RuntimeLog.LogType.Debug, "release task source token and animation is completed");
                CloseAnimation.Completed -= Closed;
                CloseAnimation = null;
                source.SetResult();
            }

            CloseAnimation = new DoubleAnimation(0, TimeSpan.FromMilliseconds(300));
            CloseAnimation.Completed += Closed;
            BeginAnimation(OpacityProperty, CloseAnimation);

            await source.Task;

            return true;
        });

        MainViewModel.RuntimeLogLift = (_bool) =>
        {
            if (!MyApp.MyLog.IsOpenlogFace() && _bool)
            {
                MyApp.MyLog.OpenConsolute(_bool);
            }
            else if (MyApp.MyLog.IsOpenlogFace() && !_bool)
            {
                MyApp.MyLog.OpenConsolute(_bool);
            }
        };

        MainViewModel.WinDragMoveLift = () =>
        {
            this.DragMove();
        };

        MainViewModel.WinActionLift = (type) =>
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                switch (type)
                {
                    case 0:
                        if (this.WindowState == WindowState.Normal)
                        {
                            this.WindowState = WindowState.Maximized;
                        }
                        else
                        {
                            this.WindowState = WindowState.Normal;
                        }
                        break;

                    case 1:
                        this.WindowState = WindowState.Minimized;
                        break;

                    case 2:
                        this.Close();
                        break;
                }
            });
        };

        Loaded += LoadComplete;
    }

    protected void LoadComplete(object sender, RoutedEventArgs e)
    {
        MainViewModel?.Init(); //  Init 方法中存在弹框UI组件展示代码，确保主程序UI加载完毕之后再执行Init，防止弹框展示异常

        DoubleAnimation CloseAnimation = new DoubleAnimation(1, TimeSpan.FromMilliseconds(300));
        BeginAnimation(OpacityProperty, CloseAnimation);

        Loaded -= LoadComplete;
    }
}